package com.lxf.project;

import com.lxf.project.inteface.*;
import com.lxf.project.inteface.entity.Order;
import com.lxf.project.inteface.entity.Student;
import org.apache.dubbo.rpc.AsyncContext;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class XmlAppConsumer {

    public static void main(String[] args) throws Exception {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
        context.start();
        final IHelloWorld  helloWorld = context.getBean("helloservice",IHelloWorld.class);
        final IOrderService  orderService = context.getBean("orderService", IOrderService.class);
        final AsyncService asyncService = context.getBean("asyncService", AsyncService.class);
        final CallbackService callbackService = context.getBean("callbackService", CallbackService.class);

//      int count = 0;
//      while (true) {
//          count++;
        try {
//                String name = helloWorld.sayHello("dubbo consumer" +count);
//                System.out.println(name);
//                Thread.sleep(1000);
            //            if (count == 1000) {
            //                throw new Exception();
            //            }

//                Student student = new Student();
//                student.setAge(18);
//                student.setName("某某人");
//                student.setSex(1);
//                helloWorld.addStudent(student);
//                orderService.update(0);
//                    Order order = new Order();
//                    order.setUserName("232");
//                    order.setAddress("232");
//                    order.setOrderNun("232");
//                    orderService.save(order);


//                System.out.println( );asyncService.sayHello("hahha ");



//                orderService.$echo();
//                break;
//                Thread.sleep(1000000);
//                final AsyncContext asyncContext = RpcContext.startAsync();
//                new Thread(()->{
//                    // 如果要使用上下文，则必须要放在第一句执行
//                    asyncContext.signalContextSwitch();
//                    try {
//                        Thread.sleep(500);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//
//                    // 写回响应
//                    asyncContext.write("Hello " + name + ", response from provider.");
//                }).start();

//
//              CompletableFuture<String> message =  asyncService.sayHello("niahobangagn");
//              message.thenAccept((result) -> {
//                  System.out.println("price: " + result);
//              });
//                // 如果执行异常:
//              message.exceptionally((e) -> {
//                  e.printStackTrace();
//                  return null;
//              });
//            while(true) {
//
                callbackService.addListener("foo.bar", new CallbackListener() {
                    public void changed(String msg) {
                        System.out.println("callback1:" + msg);
                    }
                });
//            }




        } catch (RpcException e) {
            e.printStackTrace();
            ConstraintViolationException ve = (ConstraintViolationException) e.getCause(); // 里面嵌了一个ConstraintViolationException
            Set<ConstraintViolation<?>> violations = ve.getConstraintViolations(); // 可以拿到一个验证错误详细信息的集合
            System.out.println(violations);
        }
    }
}
